/*
 * CONSOLE MENAGEMENT EVENTS
 */

bind('addAction', function (event, data) {
	if (!window.backframe_actions)
		window.backframe_actions = new Object();
		
	window.backframe_actions[data.name] = data.handler;
});

bind('delAction', function (event, data) {
	if (!window.backframe_actions)
		return;
		
	delete window.backframe_actions[data.name];
});

bind('clrActions', function (event, data) {
	if (!window.backframe_actions)
		return;
		
	window.backframe_actions = new Object();
});

bind('addChannel', function (event, data) {
	if (!window.backframe_channels)
		window.backframe_channels = new Object();
	
	var channel = ZombieAPI.channel(data.location);
	channel.onlist = function (clients) {
		window.backframe_channels[data.name].clients = new Object();
		for (var index = 0; index < clients.length; index++)
			window.backframe_channels[data.name].clients[clients[index]] = new Object();
	}
	channel.onenum = function (clients) {
		window.backframe_channels[data.name].clients = new Object();
		for (var client in clients)
			window.backframe_channels[data.name].clients[client] = clients[client];
	}
	
	window.backframe_channels[data.name] = {channel: channel, clients: new Object()};
});

bind('delChannel', function (event, data) {
	if (!window.backframe_channels)
		return;
		
	delete window.backframe_channels[data.name];
});

bind('clrChannels', function (event, data) {
	if (!window.backframe_channels)
		return;
		
	window.backframe_channels = new Object();
});

bind('addClient', function (event, data) {
	if (!window.backframe_channels)
		return;
	
	if (!window.backframe_channels[data.channel].clients)
		window.backframe_channels[data.channel].clients = new Object();
		
	window.backframe_channels[data.channel].clients[data.name] = data.data;
});

bind('delClient', function (event, data) {
	if (!window.backframe_channels && !window.backframe_channels[data.channel].clients)
		return;
		
	delete window.backframe_channels[data.channel].clients[data.name];
});

bind('clrClients', function (event, data) {
	if (!window.backframe_channels)
		return;
		
	if (data.channel && window.backframe_channels[data.channel].clients)
		window.backframe_channels[data.channel].clients = new Object();
		
	if (data.channels)
		for (var index = 0; index < data.channels.length; index++)
			window.backframe_channels[data.channels[index]].clients = new Object();
});

/*
 * CONSOLE VISUAL MENAGEMENT EVENTS
 */

bind('selectAction', function (event, data) {
	if (!window.backframe_actions)
		return;
		
	window.backframe_actions[data.action](data);
});

bind('populateActions', function (event, data) {
	$('select[@name="actions"]').html('');
	
	for (var action in window.backframe_actions)
		$('select[@name="actions"]').addOption(action, action, false);
});

bind('selectChannels', function (event, data) {
	var channels = $('select[@name="channels"]').selectedOptions();
	
	for (var index = 0; index < channels.length; index++)
		window.backframe_channels[channels[index]].channel.enum();
	
	populate_clients(channels);
});

bind('populateChannels', function (event, data) {
	$('select[@name="channels"]').html('');
	
	for (var channel in window.backframe_channels)
		$('select[@name="channels"]').addOption(channel, channel, false);
});

bind('selectClients', function (event, data) {});

bind('populateClients', function (event, data) {
	if (!window.backframe_channels)
		return;
		
	var clients = $('select[@name="clients"]').selectedOptionsByName();
	
	$('select[@name="clients"]').html('');
	
	for (var index = 0; index < data.channels.length; index++) {
		for(var client in window.backframe_channels[data.channels[index]].clients)
			$('select[@name="clients"]').addOption(data.channels[index], client, false);
	}
	
	$('select[@name="clients"] option').each(function () {
		$(this).attr('title', 'IP: ' + window.backframe_channels[$(this).val()].clients[$(this).html()]._client_ip);
	});
	
	$('select[@name="clients"]').selectOptionsByName(clients);
	$('select[@name="clients"] option').Tooltip({track: true, delay: 0, extraClass: 'message'});
});

/*
 * VISUAL EVENTS
 */

bind('show', function (event, data) {
	if (data.container == 'channels')
		$('#channels-container').show();
	else if (data.container == 'clients')
		$('#clients-container').show();
	else {
		$('#channels-container').show();
		$('#clients-container').show();
	}
});

bind('hide', function (event, data) {
	if (data.container == 'channels')
		$('#channels-container').hide();
	else if (data.container == 'clients')
		$('#clients-container').hide();
	else {
		$('#channels-container').hide();
		$('#clients-container').hide();
	}
});


bind('render', function (event, data) {
	var wrapped = (data.wrapped == undefined)?true:false;
	$('#action-container').html(wrapped?'<div class="sub-view">' + data.code + '</div>':data.code);
});

/*
 * GENERAL EVENTS
 */

bind('loadScript', function (event, data) {
	var script = document.createElement('script');
	script.defer = 'true';
	script.type = 'text/javascript';
	script.src = data.location;
	document.body.appendChild(script);
});

bind('sendMessage', function (event, data) {
	for (var index = 0; index < data.clients.length; index++)
		window.backframe_channels[data.clients[index].channel].channel.push(data.clients[index].client, data.message);
});
